package com.liuyucheng.idea_springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;


//配置类
@Configuration
// 开启Swagger2的自动配置
@EnableSwagger2
public class SwaggerConfig {

    private boolean enableSwagger = true;

    /**
     * 配置docket以配置Swagger具体参数
     */
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors 配置要扫描接口的方式
                //basePackage 指定要扫描的包
                //any 扫描所有
                //none 都不扫描
                //withClassAnnotation,withMethodAnnotation 只扫描带指定注解的接口，一个针对类，一个针对方法
                .apis(RequestHandlerSelectors.basePackage("com.liuyucheng.idea_springboot.controller"))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                //过滤
//                .paths(PathSelectors.ant("com.liuyucheng.idea_springboot.controller"))
                .build()
                //是否启用swagger，如果false：😱 Could not render e, see the console.
                .enable(enableSwagger)
                .groupName("A组")
                ;
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.regex("/swagger/.*"))
                .build()
                .groupName("B组")
                ;
    }

    /**
     * 自定义api的信息
     * @return 信息
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact(
                "刘宇诚",
                "http://localhost:9000/SpringbootDemo",
                "527520136@qq.com");

        return new ApiInfo(
                "刘宇诚的Api文档",
                "此文档是SpringBootDemo后端服务的接口文档",
                "1.0",
                "http://localhost:9000/SpringbootDemo",
                contact,
                "Apache 2.0",
                "https://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
